package com.siteseat.common.misc;

import java.io.File;
import java.io.IOException;

/**
 *
 */
public class Shutdown extends Shutdownable {

  /**
   * 
   *
   * @param args
   */
  public void execute( String[] args ) throws Exception {

    // Wait for shutdown
    String home = determineHome();
    if( home == null ) {
      System.exit( 1 );
      return;
    }
    
    File shutdownSignalFile = resolveShutdownSignalFile();
    if( shutdownSignalFile == null ) {
      System.exit( 2 );
      return;
    }    
    
    int i = 2;
    System.out.println( "It may take up to " + i * getShutdownCheckInterval() + " ms to response." );
    
    while( shutdownSignalFile.exists() && i-- > 0 ) {
      Thread.sleep( getShutdownCheckInterval() );
    }
    
    if( shutdownSignalFile.exists() ) {
      System.out.println( "Shutdown FAILED. Please retry if the server is still running." );
    } else {
      System.out.println( "Shutdown DONE." );
    }
  }
  
  /**
   *
   * @param args
   * @throws IOException 
   */
  public static void main( String[] args ) throws Exception {
    Shutdown t = new Shutdown();    
    t.execute( args );
  }

}
